<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/lib/theme-chalk/index.css" />
  <!--<link rel="stylesheet" href="../../styles/common.css" />-->
  <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
<div class="addBrand-container" id="admin-add-app">
  <div class="container">
    <el-form ref="ruleForm" :model="ruleForm" :inline="false" label-width="180px" class="demo-ruleForm">
      <el-form-item label="班级名:" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入班级名" maxlength="20"/>
      </el-form-item>
      tips:班级名称一旦创建不许修改，如需修改请联系管理员！
      <el-form-item label="教师:" prop="teacher">
        <el-input v-model="ruleForm.teacher" placeholder="请输入教师" maxlength="20"/>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="ruleForm.description" placeholder="请输入描述"></el-input>
      </el-form-item>
      <div class="subBox address">
        <el-form-item>
          <el-button  @click="goBack()">
            取消
          </el-button>
          <el-button type="primary" @click="submitForm('ruleForm', false)">
            保存
          </el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/lib/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/clazz.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script>
  new Vue({
    el: '#admin-add-app',
    data() {
      return {
        id: '',
        actionType : '',
        ruleForm : {
          name:'',
          teacher:'',
          description:''
        }
      }
    },

    created() {
      this.id = requestUrlParam('id');//从网址上解析参数
      this.actionType = this.id ? 'edit' : 'add';
      if (this.id) {
        this.init()
      }
    },
    mounted() {
    },
    methods: {
      async init () {
        //发起一个异步请求，获取用户信息
        queryClazzById(this.id).then(res => {
          if (String(res.code) === '1') {
            // console.log(res.data);
            this.ruleForm = res.data;
            // this.ruleForm.password = ''
          } else {
            this.$message.error(res.msg || '操作失败')
          }
        })
      },
      submitForm (formName, st) {
        var _this = this
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.actionType === 'add') {
              //params = 数据
              const params = {
                ...this.ruleForm,
              };
              /*找到发送请求的方法*/
              addClazz(params).then(res => {
                if (res.code === 1) {
                  this.$message.success('员工添加成功！');
                  if (!st) {
                    this.goBack()
                  } else {
                    this.ruleForm = {
                    }
                  }
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            } else {
              const params = {
                ...this.ruleForm,
              };
              editClazz(params).then(res => {
                if (res.code === 1) {
                  this.$message.success('班级信息修改成功！');
                  setTimeout(function (){
                    _this.goBack()
                  },1000);

                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            }
          } else {
            console.log('error submit!!');
            return false
          }
        })
      },
      goBack(){
        window.parent.menuHandle({
          id: '4',
          url: '/back/page/clazz/list.html',
          name: '班级管理'
        },false)
      }
    }
  })
</script>
</body>
</html>